package com.fitzone.web.controller.mini;

import com.fitzone.common.context.UserContext;
import com.fitzone.common.result.Result;
import com.fitzone.common.utils.JwtUtil;
import com.fitzone.common.utils.QRCodeUtil;
import com.fitzone.model.dto.UserDTO;
import com.fitzone.model.entity.User;
import com.fitzone.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 小程序微信登录
 */
@Api(tags = "微信登录管理")
@RequiredArgsConstructor
@RestController("miniAuthController")
@RequestMapping("/mini/auth")
public class MiniAuthController {
    private final UserService userService;

    @ApiOperation("微信登录")
    @PostMapping("/login")
    public Result wxLogin(@RequestBody Map<String, String> wxParams) {
        String code = wxParams.get("code");
//        User user = userService.wxLogin(code);
//        String token = JwtUtil.createToken(user.getId(), user.getOpenid());
        String token = JwtUtil.createToken(1732751339618930689L, "o2Rfk5esG6HVlX7VpOSaB5gk9KcI");

        HashMap<String, String> res = new HashMap<>();
        res.put("token", token);
        return Result.ok(res);
    }

    @ApiOperation("修改用户")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody UserDTO userDTO) {
        Long currentId = UserContext.getCurrentId();
        userDTO.setId(currentId);
        userService.updateUser(userDTO);
        return Result.ok();
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/getUser")
    public Result getUser(){
        Long currentId = UserContext.getCurrentId();
        User user = userService.getById(currentId);
        return Result.ok(user);
    }

    @ApiOperation("获取会员用户信息")
    @GetMapping("/getVipUser")
    public Result getVipUser(){
        Long currentId = UserContext.getCurrentId();
        User user = userService.getById(currentId);
        if (user.getVip() == null){
            return Result.fail("您不是会员 请联系工作人员或者充值会员后进门");
        }
        return Result.ok(user);
    }

    @ApiOperation("生成进门二维码")
    @GetMapping("/authQRCode")
    public Result authQRCode() {
        String content = "/mini/auth/getVipUser";
        String qrcode = QRCodeUtil.getBase64QRCode(content);
        return Result.ok(qrcode);
    }

}
